/**
 * Created by haocongshun on 2016/7/29.
 */
$(function () {
    $("#projectTeamTop").show();
    $("#projectTeamTable").show();
    $("#memberTop").hide();
    $("#memberTable").hide();
    getAll();
    bindSee();
});
function bindSee() {
    $(".see").click(function () {
        $("#projectTeamTop").hide();
        $("#projectTeamTable").hide();
        $("#memberTop").show();
        $("#memberTable").show();
        $("#memberTable tr:not(:first)").html("");
        var projectTeamID = $(this).parent().parent().children(".id").text();
        var projectTeamName= $(this).parent().parent().children(".name").text();
        var dataSee = {};
        dataSee.projectTeamID = projectTeamID;
        dataSee.flag = "see";
        var url = "/TaskCollection/ProjectTeam/projectTeam.wst";
        $.post(url,dataSee,function (result) {
            var leader = result.projectTeam.projectTeamLeader.student_name;
            $("#memberTop span").html("项目小组名称："+result.projectTeam.projectTeam_name+
                                        "&nbsp;&nbsp;&nbsp;&nbsp;容量："+result.projectTeam.projectTeam_size+
                                        "&nbsp;&nbsp;&nbsp;&nbsp;组长："+(leader==null?"待定":leader));
            for(var i=0;i<result.see.length;i++){
                var mark="";
                if(leader!=null&&leader==result.see[i].student_name){
                    mark="取消组长";
                }else {
                    mark="设为组长";
                }
                var newrow = "<tr>" +
                    "<td class='id'>"+result.see[i].student_id+"</td>" +
                    "<td class='name'>"+result.see[i].student_name+"</td>" +
                    "<td>"+result.see[i].student_gender+"</td>" +
                    "<td>"+result.see[i].student_contact+"</td>" +
                    "<td><input class='btn setLeader' type='button' value='"+mark+"'></td> " +
                    "</tr>";
                $("#member tr:last").after(newrow);
                
            }
        },"json");
        bindBack();
        //设为组长按钮绑定
        bindLeader(dataSee);
    });
}
//绑定设置组长按钮
function bindLeader(lastData) {
    
    $(".setLeader").click(function () {
        var dataleader= {};
        var msg="";
        dataleader.flag = "leader";
        dataleader.projectTeamID = lastData.projectTeamID
        if($(this).val()=="设为组长"){
            dataleader.leaderID = $(this).parent().parent().children(".id").text();
        }else{
            dataleader.leaderID = 0;
        }
        var url = "/TaskCollection/ProjectTeam/projectTeam.wst";
        $.ajaxSetup({
            async : false
        });
        $.post(url,dataleader,function (result) {
            if(result.msg==false){
                alert("请求失败！");
            }else{
               msg = result.msg;
            }
        },"json");
        $(this).val(msg);
        var info = $("#memberTop span").text();
        var name = $(this).parent().parent().children(".name").text();
        if(msg=="设为组长"){
            info = info.replace(info.substring(info.indexOf("组长：")),"组长：待定");
        }
        if(msg=="取消组长"){
            info = info.replace(info.substring(info.indexOf("组长：")),"组长："+name);
        }
        $("#memberTop span").html(info);
        
    });
}
//绑定返回按钮
function bindBack() {
    $("#back").click(function () {
        $("#projectTeamTop").show();
        $("#projectTeamTable").show();
        $("#memberTop").hide();
        $("#memberTable").hide();
        $("#projectTeam tr:not(:first)").html("");
        getAll();
        bindSee();
    });
}
function getAll() {
    var dataAll = {};
    dataAll.flag = "getAll";
    var url = "/TaskCollection/ProjectTeam/projectTeam.wst";
    $.ajaxSetup({
        async : false
    });
    $.post(url,dataAll,function (result) {
        for(var i = 0;i < result.getAll.length;i++){
            var leader=result.getAll[i].projectTeamLeader.student_name;
            leader =leader==null?"":leader;
            var deputyLeader=result.getAll[i].projectDeputyTeamLeader.student_name;
            deputyLeader = deputyLeader==null?"":deputyLeader;
            var newrow = "<tr>" +
                "<td class='id'>"+result.getAll[i].projectTeam_id+"</td>" +
                "<td class='name'>"+result.getAll[i].projectTeam_name+"</td>" +
                "<td>"+result.getAll[i].projectTeam_count+"</td>" +
                "<td>"+leader+"</td>" +
                "<td>"+deputyLeader+"</td>" +
                "<td><input class='btn see'type='button' value='查看详情'></td> " +
                "</tr>";
            $("#projectTeam tr:last").after(newrow);
        }
    },"json");
}